#!/usr/bin/env python3
"""
项目状态检查工具
检查 TAPD 季度总结系统的安装和配置状态
"""

import os
import sys
from pathlib import Path


def check_project_status():
    """检查项目状态"""
    print("🔍 TAPD 季度总结系统 - 项目状态检查")
    print("=" * 50)

    project_root = Path(__file__).parent
    issues = []

    # 1. 检查核心文件
    core_files = [
        "src/main_simple.py",
        "src/config/settings.py",
        "demo.py",
        ".env.example",
        "requirements.txt",
        "README.md"
    ]

    print("\n📁 核心文件检查:")
    for file_path in core_files:
        full_path = project_root / file_path
        if full_path.exists():
            print(f"  ✅ {file_path}")
        else:
            print(f"  ❌ {file_path} - 缺失")
            issues.append(f"缺少文件: {file_path}")

    # 2. 检查环境变量配置
    print("\n🔧 环境配置检查:")
    env_file = project_root / ".env"
    if env_file.exists():
        print("  ✅ .env 文件存在")

        # 读取环境变量
        with open(env_file) as f:
            env_content = f.read()

        if "DEEPSEEK_API_KEY=your_deepseek_api_key_here" in env_content:
            print("  ⚠️ DEEPSEEK_API_KEY 未配置（使用默认值）")
            issues.append("需要设置真实的 DEEPSEEK_API_KEY")
        elif "DEEPSEEK_API_KEY=" in env_content:
            print("  ✅ DEEPSEEK_API_KEY 已配置")
        else:
            print("  ❌ DEEPSEEK_API_KEY 配置缺失")
            issues.append("缺少 DEEPSEEK_API_KEY 配置")
    else:
        print("  ❌ .env 文件不存在")
        print("  💡 请运行: cp .env.example .env")
        issues.append("需要创建 .env 文件")

    # 3. 检查 Python 依赖
    print("\n📦 Python 依赖检查:")
    try:
        import autogen_agentchat
        print(f"  ✅ autogen-agentchat ({autogen_agentchat.__version__})")
    except ImportError:
        print("  ❌ autogen-agentchat 未安装")
        issues.append("需要安装 autogen-agentchat")

    try:
        import autogen_ext
        print("  ✅ autogen-ext")
    except ImportError:
        print("  ❌ autogen-ext 未安装")
        issues.append("需要安装 autogen-ext")

    try:
        import openai
        print(f"  ✅ openai ({openai.__version__})")
    except ImportError:
        print("  ❌ openai 未安装")
        issues.append("需要安装 openai")

    try:
        import dotenv
        print("  ✅ python-dotenv")
    except ImportError:
        print("  ❌ python-dotenv 未安装")
        issues.append("需要安装 python-dotenv")

    # 4. 检查目录结构
    print("\n📂 目录结构检查:")
    required_dirs = [
        "src",
        "src/config",
        "src/agents",
        "src/modules",
        "src/modules/tapd-capture"
    ]

    for dir_path in required_dirs:
        full_path = project_root / dir_path
        if full_path.exists():
            print(f"  ✅ {dir_path}/")
        else:
            print(f"  ❌ {dir_path}/ - 缺失")
            issues.append(f"缺少目录: {dir_path}")

    # 5. 输出总结
    print("\n" + "=" * 50)
    if not issues:
        print("🎉 项目状态良好！所有检查项均通过。")
        print("\n🚀 可以开始使用:")
        print("  1. 配置 .env 中的 DEEPSEEK_API_KEY")
        print("  2. 运行演示: python demo.py")
        print("  3. 生成报告: python src/main_simple.py")
    else:
        print(f"⚠️ 发现 {len(issues)} 个问题需要解决:")
        for i, issue in enumerate(issues, 1):
            print(f"  {i}. {issue}")
        print("\n💡 解决方案:")
        print("  - 安装依赖: pip install -r requirements.txt")
        print("  - 创建配置: cp .env.example .env")
        print("  - 配置 API Key: 编辑 .env 文件")

    return len(issues) == 0


if __name__ == "__main__":
    success = check_project_status()
    sys.exit(0 if success else 1)